<template>
  <div>
    <!-- 面包屑-->
    <el-breadcrumb separator-class='el-icon-arrow-right'>
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>订单管理</el-breadcrumb-item>
      <el-breadcrumb-item>订单列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>
    <el-card>
      <el-row>
        <el-col :span='8'>
          <el-input placeholder='请输入需要查询的订单号' v-model='queryInfo.query' clearable>
            <el-button slot='append' icon='el-icon-search' @click='getOrderList'></el-button>
          </el-input>
        </el-col>
      </el-row>
      <el-divider></el-divider>
      <el-table :data='orderList' border stripe>
        <el-table-column type='index' align='center' label='#'></el-table-column>
        <el-table-column prop='order_number' align='center' label='订单编号'></el-table-column>
        <el-table-column prop='order_price' align='center' width='80px' label='订单价格'></el-table-column>
        <el-table-column label='是否支付' width='90px' align='center'>
          <template v-slot='scope'>
            <el-tag type='danger' size='mini' v-if='scope.row.pay_status==="0"'>未支付</el-tag>
            <el-tag type='success' size='mini' v-else>已支付</el-tag>
          </template>
        </el-table-column>
        <el-table-column prop='is_send' align='center' width='90px' label='是否发货'></el-table-column>
        <el-table-column align='center' label='下单时间' width='150px'>
          <template v-slot='scope'>
            <span>{{ scope.row.create_time | formatTime }}</span>
          </template>
        </el-table-column>
        <el-table-column label='操作' width='160px' align='center'>
          <template v-slot='scope'>
            <el-button type='primary'
                       size='mini'
                       @click='orderLocationPull(scope.row)'
                       icon='el-icon-edit'></el-button>
            <el-button type='success'
                       size='mini'
                       @click='logisticsOrderPull'
                       icon='el-icon-location'></el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页 -->
      <el-row class='row-page'>
        <el-pagination
          background
          @size-change='orderSizeChange'
          @current-change='orderCurrentChange'
          :current-page='queryInfo.pagenum'
          :page-sizes='[1, 3, 5, 10,20]'
          :page-size='queryInfo.pagesize'
          layout='total, sizes, prev, pager, next, jumper'
          :total='total'>
        </el-pagination>
      </el-row>
    </el-card>
    <!-- 修改地址对话框 -->
    <el-dialog
      title='修改地址'
      @close='clearOrderLocation'
      :visible.sync='locationVisible'
      width='50%'>
      <!-- 表单信息 -->
      <el-form :model='orderForm'
               :rules='orderFormRules'
               ref='orderFormRef'
               label-width='120px'>
        <el-form-item label='省市区/县' prop='address1'>
          <el-cascader
            clearable
            v-model='orderForm.address1'
            :options='cityDate'
            :props='orderCasOption'
            @change='handleOrderChange'></el-cascader>
        </el-form-item>
        <el-form-item label='详细地址' prop='address2'>
          <el-input v-model='orderForm.address2'></el-input>
        </el-form-item>
      </el-form>
      <span slot='footer' class='dialog-footer'>
    <el-button @click='locationVisible = false'>取 消</el-button>
    <el-button type='primary' @click='updateOrderLocation'>确 定</el-button>
  </span>
    </el-dialog>
    <!-- 查看物流信息 -->
    <el-dialog
      title='物流信息'
      :visible.sync='logisticsVisible'
      width='50%'>
      <!-- 时间线 -->
      <el-timeline :reverse='true'>
        <el-timeline-item
          v-for='(item,i) in logisticsForm'
          :key='i'
          :timestamp='item.ftime'>
          {{ item.context }}
        </el-timeline-item>
      </el-timeline>
    </el-dialog>
  </div>
</template>

<script>
import cityDate from '../../plugins/citydata'

export default {
  name: 'Order',
  data() {
    return {
      // 城市编码
      cityDate,
      // 获取订单数据的请求参数
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 5
      },
      // 订单数据列表
      orderList: [],
      // 数据总数
      total: 0,
      // 修改地址对话框
      locationVisible: false,
      // 修改地址的表单
      orderForm: {
        address1: [],
        address2: ''
      },
      // 修改地址的验证规则
      orderFormRules: {
        address1: [
          { required: true, message: '请选择省市区', trigger: 'blur' }
        ],
        address2: [
          { required: true, message: '请选择县', trigger: 'blur' }
        ]
      },
      // 地区选择的配置
      orderCasOption: {
        expandTrigger: 'hover',
        value: 'value',
        label: 'label',
        children: 'children'
      },
      // 查看物流对话框
      logisticsVisible: false,
      // 物流信息
      logisticsForm: {}
    }
  },
  created() {
    this.getOrderList()
  },
  methods: {
    // 获取订单列表
    async getOrderList() {
      const { data: res } = await this.$http.get('orders', { params: this.queryInfo })
      if (res.meta.status !== 200) {
        return this.$smg.error(res.meta.msg)
      }
      this.orderList = res.data.goods
      this.total = res.data.total
    },
    // 页码改变触发
    orderSizeChange(val) {
      this.queryInfo.pagesize = val
      this.getOrderList()
    },
    // 当前页改变触发
    orderCurrentChange(val) {
      this.queryInfo.pagenum = val
      this.getOrderList()
    },
    // 拉起修改地址对话框
    orderLocationPull(row) {
      this.orderForm = row
      this.locationVisible = true
    },
    // 确定修改地址信息
    updateOrderLocation() {
      // 请求接口暂未开放
    },
    // 选择地区
    handleOrderChange() {
    },
    // 清除修改地址
    clearOrderLocation() {
      this.$refs.orderFormRef.resetFields()
    },
    // 拉起物流信息查看框
    async logisticsOrderPull() {
      const { data: res } = await this.$http.get('/kuaidi/1106975712662')
      if (res.meta.status !== 200) {
        return this.$msg.error('获取物流信息失败')
      }
      this.logisticsForm = res.data
      this.logisticsVisible = true
    }
  }
}
</script>

<style lang='less' scoped>
.row-page {
  text-align: center;
  margin-top: 15px;
}
</style>
